<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

/**
 * 上传文件业务逻辑
 *
 * Date: 29/03/2018
 * @author George
 * @package App\Http\Controllers\System
 */
class UploadController extends Controller
{
	/**
	 * 上传文件
	 *
	 * Date: 29/03/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function upload(Request $request)
	{
		$this->validate($request, [
			'file' => 'required',
			'relation' => [
				'required',
				Rule::in(array_keys(config('system.upload.relation')))
			]
		], [
			'file.required' => '请选择要上传的文件',
			'relation.required' => '请指定文件关系',
			'relation.in' => '文件关联关系不存在',
		]);
		$file = $request->file('file');
		if ($file->isValid()) {
			$path = $file->store($request->get('relation'), 'public');
			return $this->success(['path' => Storage::url($path)]);
		}
		return $this->failed('文件上传失败');
	}
}
